package com.ruoyi.safe.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.safe.domain.TTopic;
import com.ruoyi.safe.service.ITTopicService;
import com.ruoyi.safe.vo.topic.TopicPageReqDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 话题管理Controller
 *
 * @author ruoyi
 * @date 2025-03-31
 */
@RestController
@RequestMapping("/safe/topic")
public class TTopicController extends BaseController {
    @Autowired
    private ITTopicService tTopicService;

    /**
     * 查询话题管理列表
     */
    @PreAuthorize("@ss.hasPermi('safe:topic:list')")
    @GetMapping("/list")
    public TableDataInfo list(TopicPageReqDTO tTopic) {
        startPage();
        List<TTopic> list = tTopicService.selectTTopicList(tTopic);
        return getDataTable(list);
    }

    /**
     * 导出话题管理列表
     */
    @PreAuthorize("@ss.hasPermi('safe:topic:export')")
    @Log(title = "话题管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TopicPageReqDTO tTopic) {
        List<TTopic> list = tTopicService.selectTTopicList(tTopic);
        ExcelUtil<TTopic> util = new ExcelUtil<TTopic>(TTopic.class);
        util.exportExcel(response, list, "话题管理数据");
    }

    /**
     * 获取话题管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('safe:topic:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(tTopicService.selectTTopicById(id));
    }

    /**
     * 新增话题管理
     */
    @PreAuthorize("@ss.hasPermi('safe:topic:add')")
    @Log(title = "话题管理", businessType = BusinessType.INSERT, logType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TTopic tTopic) {
        return toAjax(tTopicService.insertTTopic(tTopic));
    }

    /**
     * 修改话题管理
     */
    @PreAuthorize("@ss.hasPermi('safe:topic:edit')")
    @Log(title = "话题管理", businessType = BusinessType.UPDATE, logType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TTopic tTopic) {
        return toAjax(tTopicService.updateTTopic(tTopic));
    }

    /**
     * 删除话题管理
     */
    @PreAuthorize("@ss.hasPermi('safe:topic:remove')")
    @Log(title = "话题管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tTopicService.deleteTTopicByIds(ids));
    }

    @PreAuthorize("@ss.hasPermi('safe:topic:edit')")
    @Log(title = "话题")
    @PutMapping("/updateStatus/{topicId}/{delFlag}")
    public AjaxResult updateStatus(@PathVariable Integer delFlag, @PathVariable Long topicId) {
        return toAjax(tTopicService.updateStatus(delFlag, topicId));
    }
}
